import React from 'react';
import { useLocation, useNavigate } from "react-router-dom";
function Login(){
    let [username,setUsername] = React.useState('');
    //获取当前路径对象
    const location = useLocation();
    //获取跳转路径的方法
    const navigate = useNavigate();
    const handleSubmit = (event)=>{
        //阻止默认事件
        event.preventDefault();
        //把用户对象保存到localStorage里
        localStorage.setItem('user',JSON.stringify({username}));
        //定义一个变量，保存将要跳转的路径
        let to = '/';
        //如果当前路径有状态
        if(location.state){
            //取出状态对象中的from属性赋值给to
            to=location.state.from;
        }
        //跳转到to路径中去
        navigate(to);
    }
    return (
        <form onSubmit={handleSubmit}>
           <input value={username} onChange={event=>setUsername(event.target.value)} />
           <input type="submit"></input>
        </form>
    )
}
export default Login;